#ifndef SOCKET_H
#define SOCKET_H

#include "poll.h"

#if defined(AE_WIN32)
typedef int socklen_t;
#else
typedef int SOCKET;
#define INVALID_SOCKET -1
#define closesocket close
#endif

typedef union {
	struct sockaddr sa;
	struct sockaddr_in ipv4;
	struct sockaddr_in6 ipv6;
} socket_addr;

typedef struct {
	INHERIT_POLL_CUSTOMER;
	int type;
	int family;
	SOCKET sock;
	struct {
		unsigned int is_listen : 1;
	} flags;
	atomic_t nr_closed;

#if defined(AE_WIN32) || defined(AE_FREEBSD)
	atomic_t nr_managed;
#endif

#if defined(AE_WIN32)
	union{
		struct {
			SOCKET pending;
			SOCKET ready;
			DWORD addr_bytes;
			char addr_buf[2 * (sizeof(socket_addr) + 16)];
			IOCPOVERLAPPED ola;
		} adata;
		struct {
			WSABUF buf;
			IOCPOVERLAPPED olr;
			IOCPOVERLAPPED olw;
		} rwdata;
	};
#endif
} socket_engine;

#endif
